package com.hundsun.fcp.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.hundsun.fcp.R;
import com.hundsun.fcp.base.BaseActivity;
import com.hundsun.fcp.common.TradeRequest;
import com.hundsun.fcp.common.utils.MD5;
import com.hundsun.fcp.common.utils.StringUtils;
import com.hundsun.fcp.manager.FcpUserManager;
import com.hundsun.fcp.openapi.SmsTransCode;
import com.hundsun.fcp.openapi.trade.bean.RequestUserItemBean;
import com.hundsun.fcp.widget.EditTextWithClear;
import com.hundsun.fcp.widget.TimerButton;

public class MineModifyMobileActivity extends BaseActivity implements OnClickListener {
	private EditTextWithClear mNewMobile;
	private EditTextWithClear mSmsCode;
	private EditTextWithClear mTransPwd;
	private TimerButton mSmsButton;
	private Button mBinding;
	
	private String strNewMoblie;
	private String strSmscode;
	private String strTransPwd;
	
	private RequestUserItemBean mBean ;

	@Override
	protected int getContentLayout() {
		return R.layout.fcp_mine_modify_mobile;
	}

	@Override
	protected void initHeader() {
		super.initHeader();
		mHeaderMenu.setTitle(R.string.mine_set_binding_mobile);
	}

	@Override
	public void initView() {
		super.initView();
		mBean = new RequestUserItemBean() ;
		mNewMobile = (EditTextWithClear) findViewById(R.id.id_mobile_modify_ipt);
		mSmsCode = (EditTextWithClear) findViewById(R.id.id_msgcode_modify_ipt);
		mTransPwd = (EditTextWithClear) findViewById(R.id.id_pwd_ipt);
		mBinding = (Button) findViewById(R.id.btn_modify_confirm);
		mSmsButton = (TimerButton) findViewById(R.id.id_obtain_msgcode_modify_btn);
		
		mSmsButton.setOnClickListener(this);
		mBinding.setOnClickListener(this);
		
		mNewMobile.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			public void afterTextChanged(Editable s) {
				setMsgCodeBtnEnabled();
				vfySignBtn();
			}
		});
		mSmsCode.addTextChangedListener(mTextWatcher);
		mTransPwd.addTextChangedListener(mTextWatcher);
		
	}
	private TextWatcher mTextWatcher = new TextWatcher() {
		
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			
		}
		
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			
		}
		public void afterTextChanged(Editable s) {
			  vfySignBtn();
		}
	};
	
	/**
	 * 根据手机号码是否为空来控制 获取验证码 按钮
	 */
	private void setMsgCodeBtnEnabled() {
		strNewMoblie = mNewMobile.getText().toString().trim();
		if (mNewMobile.length() > 0 && StringUtils.isMobileNO(strNewMoblie)) {
			mSmsButton.setEnabled(true);
			mSmsButton.setBackgroundResource(R.color.msgcode_bg);
		} else {
			mSmsButton.setEnabled(false);
			mSmsButton.setBackgroundResource(R.color.signup_msgcode_bg);
		}

	}
	
	public void onClick(View v) {
		if(v == mSmsButton){
			// 已经验证非空 可直接使用
			strNewMoblie = mNewMobile.getText().toString().trim();
			TradeRequest.obtainSms(this, strNewMoblie, SmsTransCode.TRANSCODE_USERUPDTE, mSmsButton) ;
		}else if(v == mBinding) {
			//已经验证非空 可直接使用
			strNewMoblie = mNewMobile.getText().toString().trim();
			strSmscode = mSmsCode.getText().toString().trim();
			strTransPwd = mTransPwd.getText().toString().trim();
			//TODO doBinding();
			mBean.clientno = FcpUserManager.getInstance().getUserClientNo() ;
			mBean.newmobile = strNewMoblie ;
			mBean.checkcode = strSmscode ;
			mBean.userpwd = MD5.getMD5Str(strTransPwd) ;
			TradeRequest.doUpdateUserItem(this, mBean, true) ;
		}
	}
	/**
	 * 根据是否输入手机号、验证码和密码来控制确认按钮
	 */
	private void vfySignBtn() {
		strNewMoblie = mNewMobile.getText().toString().trim();
		strSmscode = mSmsCode.getText().toString().trim();
		strTransPwd = mTransPwd.getText().toString().trim();

		if (StringUtils.isEmpty(strNewMoblie) || StringUtils.isEmpty(strSmscode)
				|| StringUtils.isEmpty(strTransPwd)) {
			mBinding.setEnabled(false);
			mBinding.setBackgroundResource(R.drawable.btn_grey_unable);
		} else {
			mBinding.setEnabled(true);
			mBinding.setBackgroundResource(R.drawable.btn_red_normal);
		}
	}
}
